<template>
<div class="check12" style="background-color: #ffffff">
    <div class="search1">
          <el-button @click="toggleSelection()" style="margin-left:10px">取消选择</el-button>
            <el-button @click="allRemove">删除</el-button>
        </div>
  <el-table
   ref="multipleTable"
    tooltip-effect="dark"
     @selection-change="handleSelectionChange"
      class="table7"
      :data="tableData"
      style="width: 100%"
      max-height="500"
    >
    <el-table-column
      type="selection"
      width="55">
      </el-table-column>
     <el-table-column fixed prop="positionsLocation" label="仓库名" width="108">
       <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.positionsLocation}}</span>
            <el-input v-model="scope.row.positionsLocation" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.positionsLocation}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="maxStorage" label="总存量" width="108">
      <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.maxStorage}}</span>
            <el-input v-model="scope.row.maxStorage" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.maxStorage}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="currentStorage" label="已存量" width="108">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.currentStorage}}</span>
            <el-input v-model="scope.row.currentStorage" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.currentStorage}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remainingStorage" label="可存量" width="108">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.remainingStorage}}</span>
            <el-input v-model="scope.row.remainingStorage" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.remainingStorage}}</span>
        </template>
      </el-table-column>
       <el-table-column prop="temperature" label="温度(℃)" width="108">
        <template slot-scope="scope">
          <div v-if="scope.row.id==inputHandle.id">
            <span v-if="modifyFlag">{{scope.row.temperature}}</span>
            <el-input v-model="scope.row.temperature" v-else value="number"></el-input>
          </div>
          <span v-else>{{scope.row.temperature}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="gasConcentration" label="CO₂浓度(%)" width="108">
      </el-table-column>
      <el-table-column prop="humidity" label="湿度(%)" width="108">
      </el-table-column>
      <el-table-column prop="healthSituation" label="卫生情况" width="108">
      </el-table-column>
      <el-table-column prop="equipmentInformation" label="设备情况" width="108">
      </el-table-column>
       <el-table-column label="操作" width="108">
          <template slot-scope="scope">
            <template v-if="scope.row.id==inputHandle.id">
              <el-button
             @click="ModifyRow(scope.row)"
              v-if="modifyFlag"
              type="text"
              size="small"
        >
          修改
      </el-button>
      <el-button
          @click="save(scope.row)"
          v-else
          type="text"
          size="small"
      >
          保存
      </el-button>
        </template>

        <template v-else>
           <el-button
             @click="ModifyRow(scope.row)"
              type="text"
              size="small">
             修改
          </el-button>
        </template>
        <el-button
          @click.native.prevent="deleteRow(scope.row)"
          type="text"
          size="small"
      >
         删除
      </el-button>
      </template>
      </el-table-column>

    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.page.currentPage"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size="this.page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.page.total"
      >
      </el-pagination>
    </div>
  </div>

</template>

<script>
import axios from '../../../axios'
export default {
  inject: ['reload'],
  data () {
    return {
      tableData: [{}],
      modifyFlag: true,
      inputHandle: { },
      multipleSelection: [],
      page: {
        // 当前页码
        currentPage: 1,
        // 每页条数
        pageSize: 10,
        // 数据总条数
        total: ''
      },
      delete: {
        id: '',
        pageSize: '',
        ids: []
      }
    }
  },
  methods: {
    // 修改和保存
    save (row) {
      // 保存的修改
      console.log(row)
      this.$message.success('保存成功'),
      axios({
        url: '/dp/warehouse/update',
        method: 'POST',
        data: JSON.stringify(row)
      }).then(res => {
        console.log('ceshi')
      })
      this.modifyFlag = !this.modifyFlag
    },
    ModifyRow (row) {
      this.modifyFlag = !this.modifyFlag,
      this.inputHandle = row,
      this.role()
    },
    // 单行删除
    deleteRow (val) {
      // 删除的id
      this.delete.id = val.id
      // 刷新的页面条数,page定义要在del后
      this.delete.pageSize = this.page.pageSize
      console.log(this.delete)
      axios({
        url: '/dp/warehouse/delete',
        method: 'delete',
        data: {
          data: this.delete
        }
      }).then(res => {
        this.tabledata = res.data.data.records
        this.page.total = parseInt(res.data.data.total)
      })
    },
    // 批量删除
    allRemove () {
      this.delete.pageSize = this.page.pageSize
      axios({
        url: '/dp/warehouse/Warehouse/deleteList',
        method: 'delete',
        data: {
          data: this.delete
        }
      }).then(res => {
        this.tabledata = res.data.data.records
        this.page.total = parseInt(res.data.data.total)
      })
      this.reload()
    },
    handleSelectionChange (val) {
      var array = []
      this.multipleSelection = val
      val.forEach(element => {
        array.push(element.id)
      })
      this.delete.ids = array
    },
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 条数选择
    handleSizeChange (val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      axios({
        url: '/dp/warehouse/queryMeat',
        method: 'get',
        // 上传page参数,让后端进行分页
        params: this.page
      }).then(res => {
        // 拿到后端分页数据
        this.tableData = res.data.data.records
        // 拿到后端数据总条数
        this.page.total = parseInt(res.data.data.total)
      })
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      console.log(`当前页: ${val}`)
      axios({
        url: '/dp/warehouse/queryMeat',
        method: 'get',
        // 需要想办法传入page对象
        params: this.page
      }).then(res => {
        this.tableData = res.data.data.records
      }).catch(err => {
        this.$alert(err.response.data.msg, '请联系管理员', {
          confirmButtonText: '确定',
          callback: action => {

          }
        })
      })
    }
  },
  created () {
    console.log(this.page)
    axios({
      url: '/dp/warehouse/queryMeat',
      method: 'get',
      // 需要想办法传入page对象
      params: this.page
    }).then(res => {
      this.tableData = res.data.data.records
      this.page.total = parseInt(res.data.data.total)
      console.log(this.page.total)
    }).catch(err => {
      console.log(err.response)
      this.$alert(err.response.data.msg, '请联系管理员', {
        confirmButtonText: '确定',
        callback: action => {

        }
      })
    })
  }
}
</script>
<style lang="less" >

.check12 {
  width: 1120px;
  margin: 30px auto;
  height: 600px;
  overflow: hidden;
  .cell
{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
.el-pagination{
  .el-pagination__total{
  font-weight: bold;
  font-size: 20px;
  color:rgb(61, 59, 59);
}
.el-input__inner{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
.button{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
.el-pager
.number{
  font-weight: bold;
  font-size: 20px;

}
.el-pagination__jump{
  font-weight: bold;
  font-size: 20px;
  color: rgb(61, 59, 59);
}
}
   .search1 {
      float: left;
      margin-top: 30px;
      margin-right: 700px;

   }
    .table7{
           width: 1150px!important;
       }

 }

</style>
